import apiClient from '@/api/axios.js';

export class ArticleApi {

  // 新增文章
  static async createArticle(params) {
    const { authorID, title, content, likeCount, favoriteCount, commentCount, creationTime, recommendationIndex, status, latitude, longitude } = params;
    try {
      const response = await apiClient.post('/article', {
        authorID,
        title,
        content,
        likeCount,
        favoriteCount,
        commentCount,
        creationTime,
        recommendationIndex,
        status,
        latitude,
        longitude
      });
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  static async updateArticle(articleId, params) {
    const { authorID, title, content, likeCount, favoriteCount, commentCount, creationTime, recommendationIndex, status, latitude, longitude } = params;
    try {
      const response = await apiClient.put(`/article/${articleId}`, {
        articleID: articleId, // 添加 articleID
        authorID,
        title,
        content,
        likeCount,
        favoriteCount,
        commentCount,
        creationTime,
        recommendationIndex,
        status,
        latitude,
        longitude
      });
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 删除文章
  static async deleteArticle(articleId) {
    try {
      const response = await apiClient.delete(`/article/${articleId}`);
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 获取单个文章
  static async getArticleById(articleId) {
    try {
      const response = await apiClient.get(`/article/${articleId}`);
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 查询所有文章
  static async getAllArticles() {
    try {
      const response = await apiClient.get('/article');
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }
}
